import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangchengfu
 * @description
 * @dataTime 2021年-03月-18日 12:59:00
 **/
public class Runpro {
    static class RunThread extends Thread {
        InputStream is;
        String type;

        RunThread(InputStream is, String printType) {
            this.is = is;
            this.type = printType;
        }

        public void run() {
            try {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null)
                    System.out.println(type + ">" + line);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    public static void main(String arg[]) {
        try {
            System.out.println("cmd start");
            List<String> commands = new ArrayList<>();
            commands.add("cd /Users/ycf/Documents/产品/csap/framework/csap-framework-apidoc/apidoc-template");
            commands.add("pwd");
            commands.add("npm run docs:build");
            Process p = Runtime.getRuntime().exec("/bin/bash");
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(p.getOutputStream())), true);
            for (String line : commands) {
                out.println(line);
            }
            out.println("exit");
            new RunThread(p.getInputStream(), "INFO").start();
            new RunThread(p.getErrorStream(), "ERR").start();
            int value = p.waitFor();
            if (value == 0) {
                System.out.println("complete");
            } else {
                System.out.println("failuer");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

}
